﻿using OASystem.Attributes;
using OASystem.BLL.IService;
using OASystem.Common;
using OASystem.Model;
using OASystem.Models;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OASystem.Controllers
{
    public class FilePublicController : Controller
    {
        private IHttppostedfileService _httppostedfileService;
        public FilePublicController(IHttppostedfileService httppostedfileService)
        {
            _httppostedfileService = httppostedfileService;
        }
        // GET: FileUpload
        #region 文件上传      
        public JsonResult UploadImage(HttpPostedFileBase file)
        {
            //先定义文件
            //string fileName = Guid.NewGuid().ToString("N");
            //文件存放的路径
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource/Uploads");
           
            //如果没有文件
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = ""});
            }
            //获取文件的扩展名
            //string stuf = Path.GetExtension(file.FileName);

            //文件存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //保存文件
            string path = Path.Combine(filePath, file.FileName);
            file.SaveAs(path);
            Httppostedfile httppostedfile = new Httppostedfile();
            httppostedfile.ContentLength = file.ContentLength;
            httppostedfile.ContentType = file.ContentType;
            httppostedfile.FileName = file.FileName;
            httppostedfile.FilePath = "/Resource/Uploads/" + file.FileName;
            httppostedfile.UserId = AdminContext.context.adminUser.UserId;
            httppostedfile.ispublic = 1;

            _httppostedfileService.Add(httppostedfile);
            //返回前端
            return Json(new { Success = true, FilePath = "/Resource/Uploads/" + file.FileName, code = 0 });
        }
        #endregion      
    }
}




